package com.bole.controller;


import com.bole.dao.CarDao;
import com.bole.data.Result;
import com.bole.dto.car.TbCarDto;
import com.bole.dto.content.DicContentTypeDto;
import com.bole.dto.user.UserDto;
import com.bole.pojo.DicContentTypeDo;
import com.bole.pojo.TbCarDo;
import com.bole.pojo.TbUserDo;
import com.bole.service.CarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("car")
@Api(value = "车辆信息管理api")
public class CarController {

    @Autowired
    private CarDao carDao;

    @Resource
    private CarService carService;
    @GetMapping("getCarInfo")
    @ApiOperation(value = "查询全部车辆",notes = "查询全部车辆")
    public List<TbCarDto> getCarInfo() {
        List<TbCarDo> all = carDao.findAll();
        ArrayList<TbCarDto> carDtos = new ArrayList<>();
        for (TbCarDo tbCarDo : all) {
            TbCarDto carDto = new TbCarDto();
            BeanUtils.copyProperties(tbCarDo, carDto);
            carDtos.add(carDto);
        }
        return carDtos;
    }

    @PostMapping("updateCar")
    //审核通过为1 未通过2
    @ApiOperation(value = "审核",notes = "审核")
    @ApiImplicitParam(name = "ids[]",value = "分类id",required = true,dataTypeClass = Long.class)
    public void saveContentCateInfo(@RequestParam("ids[]") Long[] ids) {
        for (Long id : ids) {
            //先通过id查询
            TbCarDo tbCarDo = carDao.findById(id).get();
            //修改状态
            tbCarDo.setStatus(1);
            TbCarDo save = carDao.save(tbCarDo);
            TbCarDto carDto = new TbCarDto();
            BeanUtils.copyProperties(save, carDto);
        }

    }

    //修改为售出状态
    @PostMapping("updateStutsCar")
    //审核通过为1 未通过2 ,售出为4
    @ApiOperation(value = "修改为售出状态",notes = "修改为售出状态")
    @ApiImplicitParam(name = "id",value = "分类id",required = true,dataTypeClass = Long.class)
    public void updateStutsCar(@RequestParam("id") Long id) {
            //先通过id查询
            TbCarDo tbCarDo = carDao.findById(id).get();
            //修改状态
            tbCarDo.setStatus(4);
            TbCarDo save = carDao.save(tbCarDo);
            TbCarDto carDto = new TbCarDto();
            BeanUtils.copyProperties(save, carDto);
    }

    //查询审核通过的前六条
    @GetMapping("getCarInfoSix")
    @ApiOperation(value = "查询前六条",notes = "查询前六条")
    public List<TbCarDto> getCarInfoSix() {
        List<TbCarDo> all = carDao.findAllByStatus();
        ArrayList<TbCarDto> carDtos = new ArrayList<>();
        for (TbCarDo tbCarDo : all) {
            TbCarDto carDto = new TbCarDto();
            BeanUtils.copyProperties(tbCarDo, carDto);
            carDtos.add(carDto);
        }
        return carDtos;
    }


    //查询车辆信息
    @PostMapping("getCarById")
    @ApiOperation(value = "查询")
    @ApiImplicitParam(name = "carId",value = "carId",required = true,dataTypeClass = Long.class)
    public TbCarDto getCarById(@RequestParam("carId") Long carId) {
        TbCarDo carDo  = carDao.findCarById(carId);
       TbCarDto carDto = new TbCarDto();
        BeanUtils.copyProperties(carDo,carDto);
        return carDto;
    }

    @GetMapping("/getCarByUserId/{userId}")
    public Result getCarByUserId(@PathVariable("userId") Long userId) {
        try {
            List<TbCarDo> carDoList = carService.getCarByUserId(userId);
            return Result.ok().setData(carDoList);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.fail().setData("查询失败");
        }
    }

    @DeleteMapping("/revoke/{carId}")
    public Result revoke(@PathVariable("carId") Long carId) {
        try {
            carService.revoke(carId);
            return Result.ok().setData("撤销成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail().setData("撤销失败");
        }
    }
}
